<?php
require_once "../class/fenajax.class.php";
include "../dyn/connexion.php";

$f = newFen(__FILE__,"Espace d'inscription",0);

$f->displayHeader();

echo "<section class=\"main\">";

if(isset($_POST['valider'])){
	$nom = isset($_POST['nom']) ? trim($_POST['nom']) : "";
	$prenom = isset($_POST['prenom']) ? trim($_POST['prenom']) : "";
	$adresse = isset($_POST['adresse']) ? trim($_POST['adresse']) : "";
	$pseudo = isset($_POST['pseudo']) ? trim($_POST['pseudo']) : "";
	$sql=mysql_query("SELECT pseudo FROM Utilisateur WHERE pseudo='".$pseudo."' ;");
	$num_rows = mysql_num_rows($sql);	
	$mdp = isset($_POST['mdp']) ? trim($_POST['mdp']) : "";
	$mdp_confirm = isset($_POST['mdp_confirm']) ? trim($_POST['mdp_confirm']) : "";
	$mail = isset($_POST['mail']) ? trim($_POST['mail']) : "";
	$mail_confirm = isset($_POST['mail_confirm']) ? trim($_POST['mail_confirm']) : "";						
	$message = "";
	if($mdp != $mdp_confirm or $mail != $mail_confirm or $num_rows != NULL){
		$message .= "<strong>Attention</strong><br/>";
		if ($mdp != $mdp_confirm){
			$message .= "Les champs <em>mdp</em> ne sont pas égaux.";
		}
		if ($mail != $mail_confirm){
			$message .= "Les champs <em>mail</em> ne sont pas égaux.";
		}
		if ($num_rows != NULL){
			$message .= "Le pseudo est déjà utilisé.";
		}		
		echo '<div class="inscription">
				<form class="form-1" method="post" action="inscription.php">
					<p class="field">
						<label for="nom">Nom</label>
						<input type="text" name="nom" id="nom"required />	
					</p>
					<p class="field">
						<label for="prenom">Prénom</label>
						<input type="text" name="prenom" id="prenom" required />
					</p>
					<p class="field">
						<label for="adresse">Adresse</label>
						<input type="text" name="adresse" id="adresse" required/>
					</p>
					<p class="field">
						<label for="pseudo">Pseudo</label>
						<input type="text" name="pseudo" id="pseudo" required/>
					</p>
					<p class="field">
						<label for="mdp">Mot de passe</label>
						<input type="password" name="mdp" id="mdp" required/>
					</p>
					<p class="field">
						<label for="mdp_confirm">Confirmez votre mot de passe</label>
						<input type="password" name="mdp_confirm" id="mdp_confirm" required//>
					</p>
					<p class="field">
						<label for="mail">Adresse email</label>					
						<input type="email" name="mail" id="mail" required/>						
					</p>
					<p class="field">
						<label for="mail_confirm">Confirmez votre adresse email</label>
						<input type="email" name="mail_confirm" id="mail_confirm" required//>
					</p>
					<button type="submit" class="buttonConnexion" name="valider"><i class="icon-arrow-right icon-large"></i></button>
					</form>
			</div>';
	}else{
		$date = date('d/m/Y'); 
		$mdp = md5($mdp);
		if (empty ($message)){
			include "../dyn/connexion.php";
		
			mysql_query(
				"INSERT INTO Utilisateur (
				   id,
				   nom,
				   prenom,
				   pseudo,
				   motDePasse,
				   dateInscription,
				   statut,
				   email,
				   adresse
				) VALUES (
				   NULL,
				   '$nom',
				   '$prenom',
				   '$pseudo',
				   '$mdp',
				   '$date',
				   0,
				   '$mail',
				   '$adresse'
				);"
		);
	}
	$message = "<p>Inscription prise en compte ! Veuillez attendre la validation de l'admin.</p>";
}
echo $message;
}else{
echo '<div class="inscription">
	<form class="form-1" method="post" action="inscription.php">
		<p class="field">
			<label for="nom">Nom</label>
			<input type="text" name="nom" id="nom"required />	
		</p>
		<p class="field">
			<label for="prenom">Prénom</label>
			<input type="text" name="prenom" id="prenom" required />
		</p>
		<p class="field">
			<label for="adresse">Adresse</label>
			<input type="text" name="adresse" id="adresse" />
		</p>
		<p class="field">
			<label for="pseudo">Pseudo</label>
			<input type="text" name="pseudo" id="pseudo" required/>
		</p>
		<p class="field">
			<label for="mdp">Mot de passe</label>
			<input type="password" name="mdp" id="mdp" required/>
		</p>
		<p class="field">
			<label for="mdp_confirm">Confirmez votre mot de passe</label>
			<input type="password" name="mdp_confirm" id="mdp_confirm" required//>
		</p>
		<p class="field">
			<label for="mail">Adresse email</label>					
			<input type="email" name="mail" id="mail" required/>						
		</p>
		<p class="field">
			<label for="mail_confirm">Confirmez votre adresse email</label>
			<input type="email" name="mail_confirm" id="mail_confirm" required//>
		</p>
		<button type="submit" class="buttonConnexion" name="valider"><i class="icon-arrow-right icon-large"></i></button>
	</form>
</div>';
				}
echo "</section>";

?>

<?php
$f->displayFooter();	
?>
